import {Component, OnInit} from '@angular/core';
import {STColumn, STColumnButton} from '@delon/abc';
import {FormProperty, SFNumberWidgetSchema, SFSchema, SFTextareaWidgetSchema} from '@delon/form';
import {SysBackupTemplateSelectAlainSchema} from '../../sys-backup-template/sys-backup-template-select-alain/sys-backup-template-select-alain.component';
import {SysBackupService} from '../service/sys-backup.service';
import {BaseService} from '@sb/base';
import {SFComponent} from '@delon/form/src/sf.component';
import {SysBackupTemplateService} from '../../sys-backup-template/service/sys-backup-template.service';
import {NzMessageService} from 'ng-zorro-antd/message';
import {Observable} from 'rxjs';
import {SysBackup} from '../entity/sys-backup';

@Component({
    selector: 'sys-backup',
    templateUrl: './sys-backup.component.html',
    styleUrls: ['./sys-backup.component.less'],
    providers: [{
        provide: BaseService,
        useClass: SysBackupService
    }]
})
export class SysBackupComponent implements OnInit {

    logForBackupId;

    columns: STColumn[] = [
        {
            title: '',
            type: 'checkbox',
            index: 'id',
            width: '30px'
        }, {
            title: '名称',
            index: 'name',
            width: '20%',
        }, {
            title: '周期表达式',
            index: 'cron',
            width: '120px'
        }, {
            title: '启用',
            index: 'enable',
            width: '60px'
        }, {
            title: '最大生存时间',
            index: 'maxAlive',
            className: 'text-center',
            width: '120px'
        }, {
            title: '操作区',
            index: 'operate',
            className: 'text-center',
            width: '250px'
        }
    ];

    lastTemplateId: string;

    schema: SFSchema = {
        properties: {
            name: {
                type: 'string',
                title: '名称',
                minLength: 2,
                ui: {
                    spanLabel: 3,
                    spanControl: 21,
                    grid: {
                        span: 24
                    }
                }
            },
            cron: {
                type: 'string',
                title: '周期表达式'
            },
            maxAlive: {
                type: 'integer',
                title: '生存时间',
                default: 1200,
                minimum: 0,
                ui: {unit: '秒'} as SFNumberWidgetSchema
            },
            templateId: {
                type: 'string',
                title: '模板',
                ui: {
                    widget: 'sys-backup-template-select-alain',
                    notifyProp: [
                        {
                            notifyProp: 'properties',
                            listenProp: 'templateId',
                            notifyWidgetMethod: (value: any, formProperty: FormProperty, sf?: SFComponent) => {
                                if (value === this.lastTemplateId) {
                                    return;
                                }
                                this.lastTemplateId = value;
                                if (!value) {
                                    const schema = this.getNewTemplateSchema(null, undefined);
                                    delete sf.rootProperty.value.properties;
                                    sf.formData = Object.assign({}, sf.formData, sf.rootProperty.value);
                                    sf.refreshSchema(schema, sf.ui);
                                    return;
                                } else {
                                    this.sysBackupTemplateService.getSchema(value).subscribe(
                                        v => {
                                            if (v.success) {
                                                const schema = this.getNewTemplateSchema(v.message.properties, value);
                                                delete sf.rootProperty.value.properties;
                                                sf.formData = Object.assign({}, sf.formData, sf.rootProperty.value);
                                                sf.refreshSchema(schema, sf.ui);
                                            } else {
                                                this.message.info('无法获取指定模板的参数！');
                                            }
                                        }
                                    );
                                }
                            }
                        }
                    ],
                } as SysBackupTemplateSelectAlainSchema
            },
            enable: {
                type: 'boolean',
                title: '启用',
                default: false
            },
            properties: {
                type: 'object',
                properties: {},
                ui: {
                    grid: {
                        span: 24,
                    }
                },
            },
            remark: {
                type: 'string',
                title: '备注',
                ui: {
                    widget: 'textarea',
                    spanLabel: 3,
                    spanControl: 21,
                    autosize: {
                        minRows: 3,
                        maxRows: 5
                    },
                    grid: {
                        span: 24
                    }
                } as SFTextareaWidgetSchema,
            },
        },
        ui: {
            // spanLabelFixed: 120,
            spanLabel: 6,
            spanControl: 18,
            grid: {
                span: 12
            }
        },
        required: ['name', 'cron', 'templateId']
    };

    customOperateButtons: STColumnButton[] = [{
        text: '日志',
        icon: 'tag',
        type: 'none',
        click: (record, modal, comp) => {
            this.logForBackupId = record.id;
        }
    }];

    constructor(private sysBackupTemplateService: SysBackupTemplateService,
                private message: NzMessageService) {
    }

    ngOnInit() {
    }

    getNewTemplateSchema(p, defaultId) {
        this.schema.properties.properties.properties = p || {};
        this.schema.properties.properties.default = defaultId;
        return this.schema;
    }

    /**
     * 提交回调方法
     */
    commitWrapper(value: SysBackup): SysBackup {
        if (value.properties && typeof value.properties === 'object') {
            value.properties = JSON.stringify(value.properties);
        }
        return value;
    }

    /**
     * 修改窗口回调方法
     */
    recordWrapper(value: SysBackup): SysBackup | Observable<SysBackup> {
        if (value.properties && typeof value.properties === 'string') {
            value.properties = JSON.parse(value.properties);
        }
        return value;
    }

}
